package com.beasts.antpage.sh.controller;

import com.beasts.antpage.sh.entity.APGroupPage;
import com.beasts.antpage.sh.entity.APHomePage;
import com.beasts.antpage.sh.service.IAPHomePageService;
import com.beasts.antpage.sh.service.IInitDataService;
import com.beasts.antpage.core.utils.HttpClientUtil;
import com.beasts.antpage.core.utils.json.JacksonUtil;
import com.crux.cloud.app.common.httpclient.HttpClientHelper;
import com.crux.cloud.app.common.user.UserInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入口
 *
 * @author cn_zkn
 * @date 07/05/2018 8:39 AM
 * @since 1.0
 */
@Controller
@RequestMapping("init")
public class InitDataController {


    private static final String HEADER_CONTEXT_ID_KEY = "com.crux.cloud.context.cloudcontext.context_id_key";

    private static final String HEADER_COOKIE_KEY = "cookie";

    @Autowired
    private IInitDataService initDataService;
    @Autowired
    private IAPHomePageService iapHomePageService;

    /**
     * 首页初始化菜单
     *
     * @return
     */
    @RequestMapping({"/getAllMenu"})
    @ResponseBody
    public List<APGroupPage> getMenu(HttpServletRequest req) {
        String menuId = req.getParameter("menuId");
        return initDataService.findAll();
    }


    /**
     * 条件查询菜单
     *
     * @return
     */
    @RequestMapping({"/getMenuByPageType"})
    @ResponseBody
    public List<APGroupPage> getMenuByPageType(HttpServletRequest req) {
        return initDataService.findByPageType(req);
    }

    /**
     * 调用平台接口获取菜单权限
     */
    @RequestMapping("/getMenuInfoFromPF")
    @ResponseBody
    public String getCurrentUserInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        Map<String, String> paramMap = new HashMap<>();
        String url = req.getParameter("url");
//        String context_id = req.getHeader(HEADER_CONTEXT_ID_KEY);
//        String cookie_id  = req.getHeader(HEADER_COOKIE_KEY);
        String res;
        try {
            res = HttpClientHelper.sendGetData(url, "UTF-8");
            if (null == res) {
                return null;
            }
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 调用平台接口获取区划及角色
     */
    @RequestMapping("/getInitUserInfo")
    @ResponseBody
    public String getInitUserInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        Map<String, String> paramMap = new HashMap<>();
        String userId = req.getParameter("userId");
        String url = "http://127.0.0.1:8082/element/User/list?id=" + userId + "&fetchProperties=id,code,name,region[*],roles[*]";
        String res;
        try {
            res = HttpClientHelper.sendGetData(url, "UTF-8");
            if (null == res) {
                return null;
            }
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * mybatis sql测试
     *
     * @return
     */
    @RequestMapping({"/testMybatis"})
    @ResponseBody
    public List<APHomePage> TestMybatis(HttpServletRequest req) {
//        String order = req.getParameter("order");
//        List<APHomePage>  list = iapHomePageService.queryByPageOrder(order);
        List<APHomePage> list = iapHomePageService.getHomePage();
        System.out.println(list);
        return list;
    }


    @RequestMapping({"/testMybatisBySql"})
    @ResponseBody
    public List<APHomePage> TestMybatisBySql(HttpServletRequest req) {
//        String order = req.getParameter("order");
//        List<APHomePage>  list = iapHomePageService.queryByPageOrder(order);
        List<APHomePage> list = iapHomePageService.getHomePageInfo();
        System.out.println(list);
        return list;
    }

    @RequestMapping("/getByGradeNm2")
    @ResponseBody
    public List<APHomePage> getByGradeNm2(Integer pageNum, Integer pageSize) {
        //页码数
        pageNum = pageNum == null ? 1 : pageNum;
        //每页显示数
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<APHomePage> list = iapHomePageService.getHomePageInfo();
        Page page = (Page) list;
        List resultList = new ArrayList();
        Map<String, Object> data = new HashMap<>();
        data.put("result", page);
        resultList.add(data);
        return resultList;
    }

    /**
     * 一键搜索
     *
     * @return
     */
    @RequestMapping({"/getPortalBusiData"})
    @ResponseBody
    public List getPortalBusiData(HttpServletRequest req) {
        String searchKey = req.getParameter("searchKey");
        Map paramMap = new HashMap();
        paramMap.put("searchKey", searchKey);
        List list = new ArrayList();
        list = iapHomePageService.getPortalBusiData(paramMap);
        return list;
    }
}
